//
//  SKUDetailReceiveVC.m
//  ZITHER
//
//  Created by weixin on 2018/8/17.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "SKUDetailReceiveVC.h"
#import "SKUDetailReceiveTB.h"
#import "OpenGoodsSourceInfoModel.h"
#import "AcceptPriceListModel.h"

@interface SKUDetailReceiveVC ()

@property (nonatomic,strong)SKUDetailReceiveTB * tb;

@end

@implementation SKUDetailReceiveVC



- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNav];
    [self setUI];
}



-(void)loadData:(BOOL)type{
    
    if (type) {
        self.tb.page = 0;
    }else{
        self.tb.page ++;
    }
    
    if (self.refreshType == 1 ) {
        [[serviceDefualt loadcheckNewBizPriceInfo:self.goodsidNew page:self.tb.page reponseObjctClass:[OpenGoodsSourceInfoModel class]]subscribeNext:^(ResponseData* x) {
            if (x.success) {
 
                NSArray *arr = x.responseRawData[@"data"];

                [self.tb setNoMoreDataBg:YES NoDataState:NoMoreDataStateDefault];

                if (arr.count == 0 ) {
                    if (self.tb.page == 0) {
                        self.arrM = [[NSMutableArray alloc]init];
                        [self.tb setNoMoreDataBg:NO NoDataState:NoMoreDataStateDefault];
                        self.tb.noMoreDateView.title.text = @"没有更多数据";
                        [Tools showTips:@"暂无更多数据"];
                    }
                    [self.tb endRefreshing];
                    [self.tb.mj_footer endRefreshingWithNoMoreData];
                    return ;
                    
                }else{
                    if (self.tb.page == 0) {
                        self.arrM = [[NSMutableArray alloc]init];
                        for (NSDictionary *dic in arr) {
                            AcceptPriceListModel *model = [AcceptPriceListModel yy_modelWithJSON:dic];
                            [self.arrM addObject:model];
                        }
                    }else{
                        for (NSDictionary *dic in arr) {
                            AcceptPriceListModel *model = [AcceptPriceListModel yy_modelWithJSON:dic];
                            [self.arrM addObject:model];
                        }
                    }
                }
            }else{
                 [Tools showTips:x.errorInfo];
            }
            self.tb.arrM = self.arrM;
            self.tb.indexType = @"1";
            [self.tb reloadData];
            [self.tb endRefreshing];
            
        }];
        return ;
    }else if(self.refreshType == 2 ) {
        
        NSLog(@"   %@",self.goodsid);
        NSLog(@"  %ld",self.tb.page);
        if (self.lookTime.length ==0 ) {
            return;
        }
        
        NSString *STR =   [NSString stringWithFormat:@"%@",self.goodsid];
        
        [[serviceDefualt loadgetNewOpenGoodsSourceInfo:self.goodsid page:self.tb.page userEnquiryId:self.userEnquiryId lookTime:self.lookTime reponseObjctClass:[OpenGoodsSourceInfoModel class]]subscribeNext:^(ResponseData * x) {
            if (x.success) {
                
                NSArray *arr = x.responseRawData[@"data"];
                
                [self.tb setNoMoreDataBg:YES NoDataState:NoMoreDataStateDefault];
                
                if (arr.count == 0 ) {
                    if (self.tb.page == 0) {
                        self.arrM = [[NSMutableArray alloc]init];
                        [self.tb setNoMoreDataBg:NO NoDataState:NoMoreDataStateDefault];
                        self.tb.noMoreDateView.title.text = @"没有更多数据";
                        [Tools showTips:@"暂无更多数据"];
                    }
                    [self.tb endRefreshing];
                    [self.tb.mj_footer endRefreshingWithNoMoreData];
                    return ;
                    
                }else{
                    if (self.tb.page == 0) {
                        self.arrM = [[NSMutableArray alloc]init];
                        for (NSDictionary *dic in x.responseRawData[@"data"] ) {
                            OpenGoodsSourceInfoModel *model =[OpenGoodsSourceInfoModel yy_modelWithJSON:dic];
                            [self.arrM addObject:model];
                        }
                    }else{
                        for (NSDictionary *dic in x.responseRawData[@"data"] ) {
                            OpenGoodsSourceInfoModel *model =[OpenGoodsSourceInfoModel yy_modelWithJSON:dic];
                            [self.arrM addObject:model];
                        }
                    }
                }
                
            
            }else{
                 [Tools showTips:x.errorInfo];
            }
            self.tb.arrM = self.arrM;
            self.tb.indexType = @"2";
            [self.tb reloadData];
            [self.tb endRefreshing];
        }];
        
        return ;
    }
    
    if ([self.indexType isEqualToString:@"1"]) {
        
        if (self.goodsidNew.length ==0) {
            
            [[serviceDefualt loadGetBizPriceInfoList:self.goodsid page:self.tb.page userEnquiryId:self.userEnquiryId reponseObjctClass:nil]subscribeNext:^(ResponseData* x) {
                /* TODO: ================= 收到报价 =================*/
                
                if (x.success) {
                    
                    NSArray *arr = x.responseRawData[@"data"];
                    
                    [self.tb setNoMoreDataBg:YES NoDataState:NoMoreDataStateDefault];
                    
                    if (arr.count == 0 ) {
                        if (self.tb.page == 0) {
                            self.arrM = [[NSMutableArray alloc]init];
                            [self.tb setNoMoreDataBg:NO NoDataState:NoMoreDataStateDefault];
                            self.tb.noMoreDateView.title.text = @"没有更多数据";
                            [Tools showTips:@"暂无更多数据"];
                        }
                        [self.tb endRefreshing];
                        [self.tb.mj_footer endRefreshingWithNoMoreData];
                        return ;
                    }else{
                        if (self.tb.page == 0) {
                            self.arrM = [[NSMutableArray alloc]init];
                            for (NSDictionary *dic in arr) {
                                AcceptPriceListModel *model = [AcceptPriceListModel yy_modelWithJSON:dic];
                                [self.arrM addObject:model];
                            }
                        }else{
                            for (NSDictionary *dic in arr) {
                                AcceptPriceListModel *model = [AcceptPriceListModel yy_modelWithJSON:dic];
                                [self.arrM addObject:model];
                            }
                        }
                    }
                    
                    
                
                }else{
                     [Tools showTips:x.errorInfo];
                }
                
                self.tb.arrM = self.arrM;
                self.tb.indexType = @"1";
                [self.tb reloadData];
                [self.tb endRefreshing];
            }];
        } else {
            [[serviceDefualt loadcheckNewBizPriceInfo:self.goodsid page:self.tb.page reponseObjctClass:[OpenGoodsSourceInfoModel class]]subscribeNext:^(ResponseData* x) {
                if (x.success) {
                    /* TODO: ================= 收到最新报价没有数据 =================*/
                    
                    NSArray *arr = x.responseRawData[@"data"];
                    
                    
                    if (arr.count == 0) {
                        if (self.tb.page == 0) {
                            self.arrM = [[NSMutableArray alloc]init];
                            [self.tb setNoMoreDataBg:NO NoDataState:NoMoreDataStateDefault];
                            self.tb.noMoreDateView.title.text = @"没有更多数据";
                            [Tools showTips:@"暂无更多数据"];
                        }
                        [self.tb endRefreshing];
                        [self.tb.mj_footer endRefreshingWithNoMoreData];
                        return ;
                    }else{
                        if (self.tb.page == 0) {
                            self.arrM = [[NSMutableArray alloc]init];
                            for (NSDictionary *dic in arr) {
                                OpenGoodsSourceInfoModel *model = [OpenGoodsSourceInfoModel yy_modelWithJSON:dic];
                                [self.arrM addObject:model];
                            }
                        }else{
                            for (NSDictionary *dic in arr) {
                                OpenGoodsSourceInfoModel *model = [OpenGoodsSourceInfoModel yy_modelWithJSON:dic];
                                [self.arrM addObject:model];
                            }
                        }
                    }
                   
                }else{
                     [Tools showTips:x.errorInfo];
                }
                self.tb.arrM = self.arrM;
                [self.tb reloadData];
                [self.tb endRefreshing];
            }];
        }
       
    }else if ([self.indexType isEqualToString:@"3"]){
        [[serviceDefualt loadGetNoInterestList:self.goodsid page:self.tb.page reponseObjctClass:nil] subscribeNext:^(ResponseData * x) {
            if (x.success) {
                
                NSArray *arr = x.responseRawData[@"data"];
                
                [self.tb setNoMoreDataBg:YES NoDataState:NoMoreDataStateDefault];
                
                if (arr.count == 0 ) {
                    if (self.tb.page == 0) {
                        self.arrM = [[NSMutableArray alloc]init];
                        [self.tb setNoMoreDataBg:NO NoDataState:NoMoreDataStateDefault];
                        self.tb.noMoreDateView.title.text = @"没有更多数据";
                        [Tools showTips:@"暂无更多数据"];
                    }
                    [self.tb endRefreshing];
                    [self.tb.mj_footer endRefreshingWithNoMoreData];
                    return ;
                    
                }else{
                    if (self.tb.page == 0) {
                        self.arrM = [[NSMutableArray alloc]init];
                        for (NSDictionary *dic in arr) {
                            AcceptPriceListModel *model = [AcceptPriceListModel yy_modelWithJSON:dic];
                            [self.arrM addObject:model];
                        }
                    }else{
                        for (NSDictionary *dic in arr) {
                            AcceptPriceListModel *model = [AcceptPriceListModel yy_modelWithJSON:dic];
                            [self.arrM addObject:model];
                        }
                    }
                }
                
                
            }else{
                [Tools showTips:x.errorInfo];
            }
            self.tb.arrM = self.arrM;
            self.tb.indexType = @"3";
            [self.tb reloadData];
            [self.tb endRefreshing];
      }];
    }else{
        if (self.userEnquiryId.length == 0) {
            [[serviceDefualt loadGetOpenGoodsSourceInfo:self.goodsid page:self.tb.page reponseObjctClass:[OpenGoodsSourceInfoModel class]]subscribeNext:^(ResponseData* x) {
                if (x.success) {
                    
                    NSArray *arr = x.responseRawData[@"data"];
                    
                    [self.tb setNoMoreDataBg:YES NoDataState:NoMoreDataStateDefault];
                    
                    if (arr.count == 0 ) {
                        if (self.tb.page == 0) {
                            self.arrM = [[NSMutableArray alloc]init];
                            [self.tb setNoMoreDataBg:NO NoDataState:NoMoreDataStateDefault];
                            self.tb.noMoreDateView.title.text = @"没有更多数据";
                            [Tools showTips:@"暂无更多数据"];
                        }
                        [self.tb endRefreshing];
                        [self.tb.mj_footer endRefreshingWithNoMoreData];
                        return ;
                        
                    }else{
                        if (self.tb.page == 0) {
                            self.arrM = [[NSMutableArray alloc]init];
                            for (NSDictionary *dic in arr) {
                                OpenGoodsSourceInfoModel *model = [OpenGoodsSourceInfoModel yy_modelWithJSON:dic];
                                [self.arrM addObject:model];
                            }
                        }else{
                            for (NSDictionary *dic in arr) {
                                OpenGoodsSourceInfoModel *model = [OpenGoodsSourceInfoModel yy_modelWithJSON:dic];
                                [self.arrM addObject:model];
                            }
                        }
                    }
                    
                    
                }else{
                    [Tools showTips:x.errorInfo];
                }
                self.tb.arrM = self.arrM;
                self.tb.indexType = @"2";
                [self.tb reloadData];
                [self.tb endRefreshing];
                
                
          
            }];
        }else{
            
            NSLog(@"");
            if (self.lookTime.length ==0 ) {
                return;
            }
            
            [[serviceDefualt loadgetNewOpenGoodsSourceInfo:self.goodsid page:self.tb.page userEnquiryId:self.userEnquiryId lookTime:self.lookTime reponseObjctClass:[OpenGoodsSourceInfoModel class]]subscribeNext:^(ResponseData * x) {
                if (x.success) {
                    
                    
                    NSArray *arr = x.responseRawData[@"data"];
                    
                    [self.tb setNoMoreDataBg:YES NoDataState:NoMoreDataStateDefault];
                    
                    if (arr.count == 0 ) {
                        if (self.tb.page == 0) {
                            self.arrM = [[NSMutableArray alloc]init];
                            [self.tb setNoMoreDataBg:NO NoDataState:NoMoreDataStateDefault];
                            self.tb.noMoreDateView.title.text = @"没有更多数据";
                            [Tools showTips:@"暂无更多数据"];
                        }
                        [self.tb endRefreshing];
                        [self.tb.mj_footer endRefreshingWithNoMoreData];
                        return ;
                        
                    }else{
                        if (self.tb.page == 0) {
                            self.arrM = [[NSMutableArray alloc]init];
                            for (NSDictionary *dic in x.responseRawData[@"data"] ) {
                                OpenGoodsSourceInfoModel *model =[OpenGoodsSourceInfoModel yy_modelWithJSON:dic];
                                [self.arrM addObject:model];
                            }
                        }else{
                            for (NSDictionary *dic in x.responseRawData[@"data"] ) {
                                OpenGoodsSourceInfoModel *model =[OpenGoodsSourceInfoModel yy_modelWithJSON:dic];
                                [self.arrM addObject:model];
                            }
                        }
                    }
                    
                }else{
                    [Tools showTips:x.errorInfo];
                }
                self.tb.arrM = self.arrM;
                self.tb.indexType = @"1";
                [self.tb reloadData];
                [self.tb endRefreshing];

            }];

        }
        
    }
}

-(void)setNav{
    [self.baseNavigationView setHiddenNav:NO VC:self];
    [self.baseNavigationView NavigationBarStyle:1];
    self.baseNavigationView.leftButton1.hidden = YES;
    self.baseNavigationView.isShowShadow = YES;
    self.baseNavigationView.navViewStyle.title.text = self.leb.text;

    if ([self.indexType isEqualToString:@"1"]) {
        /* TODO: ================= 导航栏箭头颜色 =================*/
        QMUIButton *btnTrash = [[QMUIButton alloc]initWithFrame:CGRectMake(kScreenW-60,kStatusBarHeight+5,40,40)];
        [btnTrash setImage:[UIImage imageNamed:@"nav_btn_dislike"] forState:UIControlStateNormal];
        [self.baseNavigationView.navViewStyle addSubview:btnTrash];
        @weakify(self);
        [[btnTrash rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            SKUDetailReceiveVC *vc = [[SKUDetailReceiveVC alloc]init];
            vc.indexType = @"3";
            vc.str = @"提醒商家报价";
            vc.leb.text = @"不感兴趣报价";
            vc.goodsid = self.goodsid;
            [self.navigationController pushViewController:vc animated:YES];
        }];
    }
    
}

-(void)setUI{
    
    [self.view addSubview:self.leb];
    
    [self.view addSubview:self.tb];
    
//    self.tb.str = self.str;
    
    [self loadData:YES];
}

-(SKUDetailReceiveTB *)tb{
    if (!_tb) {
        _tb = [[SKUDetailReceiveTB alloc]initWithFrame:CGRectMake(0, NaviHeight, kScreenW, kScreenH-NaviHeight)];
        __weak __typeof(self)weakSelf = self;
        _tb.indexType = self.indexType;
        _tb.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf loadData:YES];
        }];
        _tb.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [weakSelf loadData:NO];
        }];
        _tb.goodid = self.goodsid;
        [_tb setBlockRefreshData:^{
            if (weakSelf.blockRefreshData) {
                weakSelf.blockRefreshData();
            }
        }];
        
        @weakify(self);
        [_tb setBlockRemoveCell:^(NSIndexPath* index) {
            @strongify(self);
//            [self.arrM removeObjectAtIndex:index.row];
        }];
    }
    return _tb;
}

-(QMUILabel *)leb{
    if (!_leb) {
        _leb = [[QMUILabel alloc]initWithFrame:CGRectMake(20,kStatusBarHeight+50, 200, 22)];
        _leb.text = @"接受的报价";
        _leb.font = [UIFont systemFontOfSize:22];
        _leb.textColor = [UIColor blackColor];
    }
    return _leb;
}

-(void)setGoodsid:(NSString *)goodsid{
    _goodsid = goodsid;
    self.tb.goodid = self.goodsid;
    [self loadData:YES];
}

-(NSMutableArray *)arrM{
    if (!_arrM) {
        _arrM = [[NSMutableArray alloc]init];
    }
    return _arrM;
}

-(void)setStr:(NSString *)str{
    _str = str;
//    self.baseNavigationView.navViewStyle.title.text = str;
//    self.leb.text = str;
}
-(void)setIsEnquiry:(BOOL)IsEnquiry{
    _IsEnquiry = IsEnquiry;
    self.tb.IsEnquiry = IsEnquiry;
}
-(void)setIndexType:(NSString *)indexType{
    _indexType = indexType;
    self.tb.indexType = indexType;

}
@end

